package com.example.spi.service.config;

import com.example.spi.service.IService;
import com.example.spi.service.impl.OneServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author: wukangrong
 * @date: 2020/12/31 10:50
 * @description:
 */
@Configuration
@Slf4j
public class ServiceConfig {


    @ConditionalOnMissingBean(value = {IService.class})
    @Bean
    public IService getService() {
        List<IService> iServices = SpringFactoriesLoader.loadFactories(IService.class, this.getClass().getClassLoader());
        if (CollectionUtils.isEmpty(iServices)) {
            log.info("获取配置异常,采用默认配置");
            return new OneServiceImpl();
        }
        return iServices.get(0);
    }

}
